package com.yx.yinshanjinniu.activitys;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yx.yinshanjinniu.R;
import com.yx.yinshanjinniu.base.BaseActivity;
import com.yx.yinshanjinniu.bean.TurnCardBean;
import com.yx.yinshanjinniu.constants.Constant;
import com.yx.yinshanjinniu.net.ActionCallbackListener;
import com.yx.yinshanjinniu.utils.ToastUtil;

public class TurnCardActivity extends BaseActivity implements View.OnClickListener {

    private ImageView iv_back, iv_front;
    private TextView tv_draw, tv_prize, tv_count;
    private RelativeLayout rl_back;
    private TurnCardBean mCardBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_turn_card;
    }

    @Override
    protected void findViews() {
        iv_back = $(R.id.iv_back);
        tv_draw = $(R.id.tv_draw);
        iv_front = $(R.id.iv_front);
        rl_back = $(R.id.rl_back);
        tv_prize = $(R.id.tv_prize);
        tv_count = $(R.id.tv_count);
    }

    @Override
    protected void setListeners() {
        iv_back.setOnClickListener(this);
        tv_draw.setOnClickListener(this);
        iv_front.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v == iv_back) {
            backActivity();
        } else if (v == tv_draw) {
            flipCard();
        } else if (v == iv_front) {
            flipCard();
        }
    }

    private void initData() {
        mAppAction.getTurnCardList(new ActionCallbackListener<TurnCardBean>() {
            @Override
            public void onSuccess(TurnCardBean data) {
                mCardBean = data;
                setCardStatus(true);
            }

            @Override
            public void onFailure(String errorCode, String message) {
                if (message.equals(Constant.NULL_DATA_MSG)){
                    setCardStatus(false);
                }else {
                    ToastUtil.showToast(mContext, message);
                }
            }
        });
    }

    private void flipCard() {
        loading();
        mAppAction.drawTurnCard(new ActionCallbackListener<TurnCardBean>() {
            @Override
            public void onSuccess(TurnCardBean data) {
                cancelLoading();
                mCardBean = data;
                // 正面到背面
                AnimatorSet set = new AnimatorSet();
                set.playSequentially(
                        ObjectAnimator.ofFloat(iv_front, "rotationY", 0f, 90f),
                        ObjectAnimator.ofFloat(rl_back, "rotationY", -90f, 0f)
                );
                set.setDuration(500);
                set.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        setCardStatus(true);
                        rl_back.bringToFront();
                    }
                });
                set.start();
            }

            @Override
            public void onFailure(String errorCode, String message) {
                cancelLoading();
                ToastUtil.showToast(mContext, message);
            }
        });
    }

    /**
     * 设置卡片状态
     * @param isDrawed 是否抽过奖了
     */
    private void setCardStatus(boolean isDrawed){
        if (isDrawed){
            tv_count.setText(" 0 ");
            iv_front.setVisibility(View.GONE);
            rl_back.setVisibility(View.VISIBLE);
            tv_draw.setEnabled(false);

            tv_prize.setText(mCardBean.cn);
        }else {
            tv_count.setText(" 1 ");
            iv_front.setVisibility(View.VISIBLE);
            rl_back.setVisibility(View.GONE);
            tv_draw.setEnabled(true);
        }
    }
}